package com.cn.jx.manager.service.query.Impl;

import java.util.ArrayList;
import java.util.List;

import com.cn.jx.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.manager.dao.SystemOperationRepository;
import com.cn.jx.manager.po.SystemOperation;
import com.cn.jx.manager.po.User;
import com.cn.jx.manager.service.query.IAnalysisServiceQry;
import com.cn.jx.manager.service.query.IUserServiceQry;

import com.jx.enumType.OperatEnum;
import com.jx.jpaQuery.QueryCondition;
import com.jx.util.ArrayUtil;
import com.jx.util.DateUtil;
import com.jx.util.MapObject;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月16日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("analysisServiceQry") 
@Transactional
public class AnalysisServiceImplQry extends BaseService implements IAnalysisServiceQry {

	@Autowired
	private SystemOperationRepository systemOperationDao;
	
	@Autowired
	private IUserServiceQry userServiceQry;
	
	
	@Override
	@Transactional(readOnly = true)
	public PageObj<MapObject> findOperLogByPageAndParams(QueryCondition cond) throws Exception{
		Specification<SystemOperation> speci = getSpecification(cond);
		Pageable pageable = getPageables(cond);
		Page<SystemOperation> res = systemOperationDao.findAll(speci, pageable);
		List<SystemOperation> rescontent=res.getContent();
		List<MapObject> listContent=new ArrayList<MapObject>();
		if(ArrayUtil.listNotEmpty(rescontent)){
			rescontent.forEach(r->{
				
				User u=userServiceQry.getUserById(r.getOperatUser());
				String userName="";
				if(u!=null){
					userName=u.getChineseName();
				}else{
					if(r.getOperatUser().contains("root_")){
						userName="超级用户";
					}
				}
				MapObject mp=MapObject.getInstance();
				mp.put("operatType",OperatEnum.getEnumNameByCode(r.getOperatType()));
				mp.put("operatUser", userName);
				mp.put("operatTime", DateUtil.getDateFmt(r.getOperatTime()));
				mp.put("opertTable", r.getOpertTable());
				mp.put("tableId", r.getTableId());
				mp.put("operatDesc", r.getOperatDesc());
				mp.put("id", r.getId());
				listContent.add(mp);
			});
		}
		PageObj<MapObject> pageRes = PageObj.of(res, listContent);
		return pageRes;
	}

}
